﻿Public Class FrmTipoCliente
    Dim classController As New Class_RC_Controller.rc_Clientes
    Private _codtipocliente As Integer = 0

    Public Property codtipocliente() As Integer
        Get
            Return _codtipocliente
        End Get
        Set(ByVal value As Integer)
            _codtipocliente = value
        End Set
    End Property
    Private Sub CargarDatos()
        Try
            If codtipocliente > 0 Then

                Dim ds As New DataTable
                ds = classController.get_TipoClientes_All(oiml.dbConn, codtipocliente, "")

                If Not ds Is Nothing Then
                    txtcodigo.Text = ds.Rows(0)("codtipocliente")
                    txtdescripcion.Text = ds.Rows(0)("descripcion")
                    txtlimitecredito.Text = ds.Rows(0)("limitecredito")
                    txtporcdesc.Text = ds.Rows(0)("porcdescuento")
                    btneliminar.Enabled = True
                End If
            Else
                btneliminar.Enabled = False
                txtdescripcion.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If
        If txtlimitecredito.Text = "" Then
            ErrorProvider.SetError(txtlimitecredito, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtlimitecredito, "")
        End If

        If Not IsNumeric(txtlimitecredito.Text) Then
            ErrorProvider.SetError(txtlimitecredito, "Campo numerico, Costo")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtlimitecredito, "")
        End If

        If txtporcdesc.Text = "" Then
            ErrorProvider.SetError(txtporcdesc, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtporcdesc, "")
        End If

        If Not IsNumeric(txtporcdesc.Text) Then
            ErrorProvider.SetError(txtporcdesc, "Campo numerico, Costo")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtporcdesc, "")
        End If

        Return controlCampos

    End Function

    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        If codtipocliente > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Tipo Cliente", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_TipoClientes(oiml.dbConn, codtipocliente) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Tipo Cliente", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If
    End Sub
    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            Exit Sub
        End If

        If codtipocliente = 0 Then
            codtipocliente = classController.Insert_TipoCliente(oiml.dbConn, txtdescripcion.Text, txtlimitecredito.Text, txtporcdesc.Text)
            If codtipocliente > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Tipo Cliente", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Tipo Tliente", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_TipoCliente(oiml.dbConn, codtipocliente, txtdescripcion.Text, txtlimitecredito.Text, txtporcdesc.Text) Then
                MessageBox.Show("Registro grabado con exito!", "Tipo Cliente", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Tipocliente", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub
    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub

    Private Sub FrmTipoCliente_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarDatos()
    End Sub
End Class